layui.use(['layer','jquery','form','table'],function () {
    let form = layui.form;
    let layer = layui.layer;
    let $ = layui.$;
    let table = layui.table;
    //获取所有轮播图
    getBanners();
    //返回功能
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    //根据条件查询轮播图
    function getBanners(banner) {
        $.ajax({
            url: '/banner/getBanners',
            dataType: 'json',
            data: banner,
            success: function (data) {
                //将查出来的数据渲染到轮播图列表
                bannersTableRender(data.data);
            }
        });
    }
    //将查出来的数据渲染到轮播图列表
    function bannersTableRender(data) {
        table.render({
            elem: '#slideshowsTable',
            title: '轮播图列表',
            data: data,
            limit: 10,
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox'},
                {title:'序号',align:'center',templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"imgTitle",title:"图片标题",align:'center'},
                {field:"account",title:"创建人",align:'center'},
                {field:"",title:"图片",align:'center',templet:function(data){
                        return '<img src="'+ data.imgAddress+'" />';
                    }},
                {field:"startDate",title:"开始时间",align:'center'},
                {field:"expireDate",title:"到期时间",align:'center'},
                {field:"remarks",title:"备注",align:'center'},
                {field:'',title:"操作",align:'center',toolbar: '#operation'}
            ]]
        });
    }
    //刷新功能
    $('#refresh').click(function () {
        location.reload();
    });
    //根据上线状态、图片类型查询轮播图
    form.on('submit(search)',function (obj) {
        getBanners(obj.field);
        return false;
    });
    //行内工具栏 删除和编辑功能
    table.on('tool(bannersTable)',function (obj) {
        //删除轮播图功能
        if(obj.event == 'delBannerTable'){
            layer.confirm('确定要删除' + obj.data.imgTitle + '么？',
                {icon: 3, title:'删除轮播图'},
                function(index){
                    delBanner(obj.data.carouselId);
                    layer.close(index);
                }
            );
        //更改轮播图功能
        }else if(obj.event == 'updateBannerTable'){
            layer.open({
                type: 2,
                title: '编辑轮播图',
                content: '/banner/bannerManage/bannerList/update-banner.html',
                area:['900px','600px'],
                scrollbar: false,
                offset:'auto',
                shade: 0.4,
                success: function (layero,index) {
                    var childHtml = layero.find('iframe').contents();
                    childHtml.find('#carouselId').val(obj.data.carouselId);
                },
                end: function () {
                    //getBanners();
                }
            });
        }
    });


    //根据id删除轮播图
    function delBanner(id) {
        $.ajax({
            url: '/banner/delBanner',
            data: {carouselId: id},
            type: 'post',
            dataType: 'json',
            success: function (data) {
                if(data.data){
                    getBanners()
                    layer.msg('删除成功')
                }
            }
        });
    }
});